const app = getApp()
const login_js = require('../../utils/login.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_user: false,
    is_dark: false,
    modal_show: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      is_dark: app.global_data.darkness
    })
  },
  onShow() {
    // 检查是否登录过
    if (app.global_data.user_data.openid) {
      this.setData({
        user: app.global_data.user,
        user_data: app.global_data.user_data,
        is_user: true
      })
    }
    this.setData({
      is_dark: app.global_data.darkness
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  ...login_js.methods,
  /**
   * 登录时候获取用户信息回调
   */
  async getUserInfo(e) {
    await this.getProfile()
    this.clickModalCancel()
  },
  clickModalCancel() {
    this.setData({
      is_user: true,
      modal_show: false,
    })
  },
  clickDarkness() {
    if (this.data.is_dark) {
      this.setData({
        is_dark: false
      })
      app.global_data.darkness = false
    } else {
      this.setData({
        is_dark: true
      })
      app.global_data.darkness = true
    }
  },
  clickSetting() {
    if (this.data.is_user) {
      let that = this
      wx.showModal({
        content: '要退出登录吗',
        success(res) {
          if (res.confirm) {
            wx.clearStorageSync('user_data')
            wx.clearStorageSync('user')
            app.global_data.user = {}
            app.global_data.user_data = {}
            that.setData({
              is_user: false
            })
          } 
        }
      })
    } else {
      this.setData({
        modal_show: true
      })
      return
    }
  },
  clickTest(){
    if (this.data.is_user) {
    wx.navigateTo({
      url: `/pages/test_list/test_list`
    })
    } else {
      this.setData({
        modal_show: true
      })
      return
    }
  },
  clickChallenge(){
    if (this.data.is_user) {
      wx.navigateTo({
        url: `/pages/challenge_list/challenge_list`
      })
      } else {
        this.setData({
          modal_show: true
        })
        return
      }
  },
  clickUs(){
    wx.showModal({
      title: '新手上路!',
      content: '欢迎提出BUG和建议',
      success (res) { }
    })
  },
  clickMiniGame(){
    wx.navigateTo({
      url: '/pages/minigame_list/minigame_list',
    })
  }
})